/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Engine2D.graphics.particle;

import Engine2D.graphics.GLColour;

/**
 *
 * @author Garys
 */
public class GLParticle {
    
   private GLParticleInterface particle = GLParticleFactory.getParticle("rock");
   private float x, y,vX,vY,alpha,alphaStep;
   private boolean isDead;
   private long   startTime;
   private float  lifeTime;
   
   
   /**
    * 
    * @param type
    */
   public GLParticle(String type) {
       particle = GLParticleFactory.getParticle(type);
   }
   
   /**
    * 
    * @param x
    * @param y
    * @param vX
    * @param vY
    * @param alpha
    * @param alphaStep
    * @param lifeTime
    * @param startTime
    * @param colour
    */
   public void init(float x, float y, float vX, float vY, float alpha, float alphaStep, long lifeTime, long startTime, GLColour colour) {
        this.x = x;
        this.y = y;
        this.vX = vX;
        this.vY = vY;
        this.alpha = alpha;
        this.alphaStep = alphaStep; 
        this.lifeTime = (float)lifeTime;
        this.startTime = startTime;
        particle.setColor(colour);
        isDead = false;
   }
   
   /**
    * 
    */
   public void tick() {
            
        if (System.currentTimeMillis() - startTime > lifeTime) {
            die();
        } else {
            updateAlpha();
            x += vX;
            y += vY;
        }
    }
   
   /**
    * 
    * @param xScroll
    * @param yScroll
    */
   public void render(int xScroll, int yScroll){
       if (!isDead) {
           particle.draw((int)this.x - xScroll, (int)this.y - yScroll, alpha);
        }
   }
   
   private void die() {
        isDead = true;
    }

    private void updateAlpha() {
        alpha -= alphaStep;
    }

    /**
     * 
     * @return
     */
    public boolean isDead() {
        return isDead;
    }
   
}
